package com.violet.base.server.dic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.violet.base.api.dic.dto.DicItemDTO;
import com.violet.base.server.dic.entity.DictItemEntity;
import com.violet.base.server.dic.mapper.DictItemMapper;
import com.violet.base.server.dic.service.DictItemService;
import com.violet.common.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Author: wqf
 * @Date: 2022/10/27
 * @Description:
 */
@Service
public class DictItemServiceImpl implements DictItemService {

    @Resource
    private DictItemMapper dictItemMapper;
    @Resource
    private HttpServletRequest httpServletRequest;
    /**
     * 方法描述: 新增或更新字典数
     *
     * @param dicItemDTO 入参实体
     * @author wqf
     * @date 2022/10/27 11:21
     */
    @Override
    public void saveOrUpdateDictItem(DicItemDTO dicItemDTO) {
        Long id = dicItemDTO.getId();
        String dictItemName = dicItemDTO.getDictItemName();
        Long dicTypeId = dicItemDTO.getDicTypeId();
        String dictItemValue = dicItemDTO.getDictItemValue();
        //校验字典名称是否重复
        QueryWrapper<DictItemEntity> itemNameQueryWrap = new QueryWrapper<DictItemEntity>()
                .eq("dic_type_id", dicTypeId)
                .eq("dict_item_name", dictItemName);
        DictItemEntity itemNameEntity = dictItemMapper.selectOne(itemNameQueryWrap);
        if (itemNameEntity != null && !itemNameEntity.getId().equals(id)) {
            throw new BusinessException("字典名称已存在");
        }
        //校验字典值是否重复
        QueryWrapper<DictItemEntity> itemValueQueryWrap = new QueryWrapper<DictItemEntity>()
                .eq("dic_type_id", dicTypeId)
                .eq("dict_item_value", dictItemValue);
        DictItemEntity itemValueEntity = dictItemMapper.selectOne(itemValueQueryWrap);
        if (itemValueEntity != null && !itemValueEntity.getId().equals(id)) {
            throw new BusinessException("字典值已存在");
        }
        DictItemEntity dictItemEntity = new DictItemEntity();
        BeanUtils.copyProperties(dicItemDTO, dictItemEntity);
        if (id != null) {
            dictItemMapper.updateById(dictItemEntity);
        } else {
            dictItemMapper.insert(dictItemEntity);
        }
    }

    /**
     * 方法描述: 根据主键id删除字典数据
     *
     * @param id 主键id
     * @author wqf
     * @date 2022/10/27 11:25
     */
    @Override
    public void deleteDictItemById(Long id) {
        DictItemEntity dictItemEntity = dictItemMapper.selectById(id);
        if (dictItemEntity == null) {
            throw new BusinessException("资源不存在");
        }
        dictItemMapper.deleteById(id);
    }

    /**
     * 方法描述: 根据字典分类编码查询字典数据列表
     *
     * @param dicTypeCode 字典分类编码
     * @return java.util.List<com.violet.base.server.dic.dto.DicItemDTO>
     * @author wqf
     * @date 2022/10/27 11:31
     */
    @Override
    public List<DicItemDTO> findDictItemByDicTypeCode(String dicTypeCode) {
        return dictItemMapper.selectDictItemByDicTypeCode(dicTypeCode);
    }

    /**
     * 方法描述: 根据字典分类编码查询字典数据下拉列表
     *
     * @param dicTypeCode 字典分类编码
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @author wqf
     * @date 2022/10/27 11:39
     */
    @Override
    public LinkedList<Map<String, String>> findDictItemDropDownListByDicTypeCode(String dicTypeCode) {
        String token = httpServletRequest.getHeader("token");
//        httpServletRequest.getHeader("Au")
        List<DicItemDTO> dicItemDTOList = findDictItemByDicTypeCode(dicTypeCode);
        LinkedList<Map<String, String>> resultList = new LinkedList<>();
        if (dicItemDTOList.size() > 0) {
            for (DicItemDTO dicItemDTO : dicItemDTOList) {
                Map<String, String> dataMap = new HashMap<>();
                dataMap.put("label", dicItemDTO.getDictItemName());
                dataMap.put("value", dicItemDTO.getDictItemValue());
                resultList.add(dataMap);
            }
        }
        return resultList;
    }


}


